package com.lanxiao.cloud.modules.user.application.query;

import cn.hutool.core.collection.CollUtil;
import com.lanxiao.cloud.common.core.utils.StringUtils;
import com.lanxiao.cloud.common.mybatis.query.QueryWrapper;
import com.lanxiao.cloud.modules.user.domain.model.User;
import java.util.Date;
import java.util.Objects;

/**
 * @author ：qiaoliang
 * @date ：2021-09-14
 */
public class UserLambdaQueryWrapper extends QueryWrapper<User> {

  public static UserLambdaQueryWrapper wrapper() {
    return new UserLambdaQueryWrapper();
  }

  public UserLambdaQueryWrapper queryWrapper(UserLambdaQuery query) {
    queryWrapper
        .and(StringUtils.isNotBlank(query.getName()),
            o->o.likeRight(User::getName, query.getName())
            .or()
            .likeRight(User::getInitials, StringUtils.getPinYinHeadChar(query.getName())))
        .eq(StringUtils.isNotBlank(query.getPhone()), User::getPhone, query.getPhone())
        .eq(Objects.nonNull(query.getSex()), User::getSex, query.getSex())
        .in(CollUtil.isNotEmpty(query.getDeptIds()), User::getDeptId, query.getDeptIds())
        .eq(Objects.nonNull(query.getState()), User::getState, query.getState());
    return this;
  }

  public UserLambdaQueryWrapper geTime(Date date) {
    queryWrapper.ge(Objects.nonNull(date), User::getCreateTime, date);
    return this;
  }

}
